package com.baseframe.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baseframe.auth.utils.AuthUtil;
import com.baseframe.tool.result.R;
import com.baseframe.user.entity.request.BaseUserRequest;
import com.baseframe.user.entity.vo.BaseUserVO;
import com.baseframe.user.service.BaseUserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 用户控制层
 */
@RestController
@RequestMapping("/back/user")
@AllArgsConstructor
public class BackBaseUserController {

    private BaseUserService baseUserService;

    /**
     *获取用户信息（登录之后）
     */
    @GetMapping("/info")
//    @Power(authorizes = {"admin","user"})
    public R<BaseUserVO> getUserInfo(){
        Long userId = AuthUtil.getUserId();
        BaseUserVO userInfo = baseUserService.getUserInfo(userId);
        userInfo.setPassword(null);
        return R.data(userInfo);
    }

    /**
     * 用户管理分页列表
     * @param page
     * @param request
     * @return
     */
    @GetMapping("/page")
    public R<Page<BaseUserVO>> getUserPage(Page<BaseUserVO> page,BaseUserRequest request){

        return R.data(baseUserService.getUserPage(page,request));
    }

    /**
     * 封禁用户
     * @param request
     * @return
     */
    @PostMapping("/seal")
    public R<Object> sealUser(@RequestBody BaseUserRequest request){

        if (request.getStatus()<0||request.getStatus()>1){
            return R.fail("用户状态数据不合法");
        }

        return baseUserService.sealUser(request)?R.success():R.fail();
    }

    /**
     * 设置用户角色
     * @param request
     * @return
     */
    @PostMapping("/set/role")
    public R<Object> setRole(@RequestBody BaseUserRequest request){

        if (request.getId()==null||request.getRoleId()==null){
            return R.fail("缺少必要请求参数");
        }

        return baseUserService.setRole(request)?R.success():R.fail();
    }

    @PostMapping("/update/password")
    public R<Object> updatePassword(@RequestBody BaseUserRequest request){


        return baseUserService.updatePassword(request.getNewPassword(),request.getOldPassword());
    }

    @PostMapping("/reset/password")
    public R<Object> resetPassword(@RequestBody BaseUserRequest request){


        return baseUserService.resetPassword(request);
    }




}
